package pe.edu.upc.xcode.food.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pe.edu.upc.xcode.food.dao.RestaurantDAO;
import pe.edu.upc.xcode.food.domain.Local;
import pe.edu.upc.xcode.food.domain.Plato;
import pe.edu.upc.xcode.food.domain.Restaurant;
@Repository
@Transactional
public class RestaurantDAOImpl implements RestaurantDAO {

    @PersistenceContext
    private EntityManager em;
	
	public RestaurantDAOImpl() {
		// TODO Auto-generated constructor stub
	}

	public Integer save(Restaurant object) {
		// TODO Auto-generated method stub
		em.persist(object);
		
		List<Local> locals = object.getLocals();
		
		for (Local local : locals) {
			local.setRestaurant(object);
			em.persist(local);
		}
		
		
		return object.getCodigo();
		
		
		
		 
		
		
	}

	public Integer update(Restaurant object) {
		// TODO Auto-generated method stub
		em.merge(object);
		return object.getCodigo();
	}

	public Integer detele(Restaurant object) {
		em.remove(object);
		return object.getCodigo();
	}

	public Restaurant find(Restaurant object) {
		// TODO Auto-generated method stub
		Query query=em.createQuery("select o from Restaurant r where r.codigo=:parameter");
		query.setParameter("parameter", object.getCodigo());
		return (Restaurant) query.getSingleResult();
	}

	public List<Restaurant> findAll() {
		Query query = em.createQuery("SELECT r from Restaurant r where e.estado='A' ");
		List<Restaurant> list= query.getResultList();
		return list;
	}

}
